﻿Imports System.ServiceModel
Imports System.IO
Imports System.Net
Imports Microsoft.WindowsAzure
Imports Microsoft.WindowsAzure.ServiceRuntime
Imports Microsoft.WindowsAzure.StorageClient
Imports Proyect_0001JFR.Dato
Imports System.Net.Sockets

' NOTE: You can use the "Rename" command on the context menu to change the class name "wcfProyect_0001" in code, svc and config file together.
Public Class wcfProyect_0001
    Implements IwcfProyect_0001

    Private Shared storageInitialized As Boolean = False
    Private Shared gate As New Object()
    Private Shared blobStorage As CloudBlobClient
    Private Shared queueStorage As CloudQueueClient
    Private archivo As New FileUpload


    Public Function GuardarArchivo(strRuta As String) As String Implements IwcfProyect_0001.GuardarArchivo

        AddHandler archivo.Unload, AddressOf Carga
        Dim retorno As String = False
        'archivo.Unload()

        'archivo.ID = strRuta
        'If archivo.HasFile Then
        '    Try
        '        InitializeStorage()
        '        ' upload the image to blob storage
        '        Dim uniqueBlobName As String = String.Format("guestbookpics/image_{0}{1}", Guid.NewGuid().ToString(), Path.GetExtension(archivo.FileName))
        '        Dim blob As CloudBlockBlob = blobStorage.GetBlockBlobReference(uniqueBlobName)
        '        blob.Properties.ContentType = archivo.PostedFile.ContentType ''FileUpload1.PostedFile.ContentType
        '        blob.UploadFromStream(archivo.FileContent)
        '        System.Diagnostics.Trace.TraceInformation("Cagar archivo '{0}' en blob storage '{1}'", archivo.FileName, uniqueBlobName)
        '        ' create a new entry in table storage
        '        Dim entry As New PruebaEntry() With {.GuestName = archivo.FileName, .Message = archivo.FileName, .PhotoUrl = blob.Uri.ToString(), .ThumbnailUrl = blob.Uri.ToString()}
        '        Dim ds As New PruebaDataSource()
        '        ds.AddPreubaEntry(entry)
        '        System.Diagnostics.Trace.TraceInformation("Added entry {0}-{1} in table storage for guest '{2}'", entry.PartitionKey, entry.RowKey, entry.GuestName)
        '        ' queue a message to process the image
        '        Dim queue = queueStorage.GetQueueReference("guestthumbs")
        '        Dim message = New CloudQueueMessage(String.Format("{0},{1},{2}", blob.Uri.ToString(), entry.PartitionKey, entry.RowKey))
        '        queue.AddMessage(message)
        '        System.Diagnostics.Trace.TraceInformation("Queued message to process blob '{0}'", uniqueBlobName)
        '        retorno = blob.Uri.ToString()
        '    Catch ex As Exception
        '        retorno = False
        '    End Try
        'End If

        Return retorno
    End Function


    Private Sub Carga(sender As Object, e As EventArgs)
        Throw New NotImplementedException
    End Sub

    Private Sub InitializeStorage()
        If storageInitialized Then
            Return
        End If
        SyncLock gate
            If storageInitialized Then
                Return
            End If
            Try
                ' read account configuration settings
                Dim storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString")
                ' create blob container for images
                blobStorage = storageAccount.CreateCloudBlobClient()
                Dim container As CloudBlobContainer = blobStorage.GetContainerReference("guestbookpics")
                container.CreateIfNotExist()
                ' configure container for public access
                Dim permissions = container.GetPermissions()
                permissions.PublicAccess = BlobContainerPublicAccessType.Container
                container.SetPermissions(permissions)
                ' create queue to communicate with worker role
                queueStorage = storageAccount.CreateCloudQueueClient()
                Dim queue As CloudQueue = queueStorage.GetQueueReference("guestthumbs")
                queue.CreateIfNotExist()
            Catch e1 As WebException
                Throw New WebException("Storage services initialization failure. " _
                  & "Check your storage account configuration settings. If running locally, " _
                  & "ensure that the Development Storage service is running.")
            End Try
            storageInitialized = True
        End SyncLock
    End Sub


End Class
